package com.onepiece.shipelves.common.utils;

import org.apache.commons.lang.time.DateUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * @author guoyanjiang
 * @date 2018/10/22
 */
public class DateUtil {

    public static String yyyyMMdd = "yyyyMMdd";
    public final static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter DATE_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // ==格式到年==
    /**
     * 日期格式，年份，例如：2004，2008
     */
    public static final String DATE_FORMAT_YYYY = "yyyy";


    // ==格式到年月 ==
    /**
     * 日期格式，年份和月份，例如：200707，200808
     */
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";

    /**
     * 日期格式，年份和月份，例如：200707，2008-08
     */
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";


    // ==格式到年月日==
    /**
     * 日期格式，年月日，例如：050630，080808
     */
    public static final String DATE_FORMAT_YYMMDD = "yyMMdd";

    /**
     * 日期格式，年月日，用横杠分开，例如：06-12-25，08-08-08
     */
    public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";

    /**
     * 日期格式，年月日，例如：20050630，20080808
     */
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    /**
     * 日期格式，年月日，用横杠分开，例如：2006-12-25，2008-08-08
     */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";


    public static final String DATE_FORMAT_YYYYM_MDD = "yyyy/MM/dd";

    /**
     * 日期格式，年月日，例如：2016.10.05
     */
    public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";

    /**
     * 日期格式，年月日，例如：2016年10月05日
     */
    public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";


    // ==格式到年月日 时分 ==

    /**
     * 日期格式，年月日时分，例如：200506301210，200808081210
     */
    public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";

    /**
     * 日期格式，年月日时分，例如：20001230 12:00，20080808 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";

    /**
     * 日期格式，年月日时分，例如：2000-12-30 12:00，2008-08-08 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";


    // ==格式到年月日 时分秒==
    /**
     * 日期格式，年月日时分秒，例如：20001230120000，20080808200808
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";

    /**
     * 日期格式，年月日时分秒，年月日用横杠分开，时分秒用冒号分开
     * 例如：2005-05-10 23：20：00，2008-08-08 20:08:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";


    // ==格式到年月日 时分秒 毫秒==
    /**
     * 日期格式，年月日时分秒毫秒，例如：20001230120000123，20080808200808456
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";


    // ==特殊格式==
    /**
     * 日期格式，月日时分，例如：10-05 12:00
     */
    public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";

    public static final int HOUR_SECOND = 3600;
    public static final int DAY_SECOND = 86400;
    public static final long YEAR_SECOND = 365 * 24 * 60 * 60;

    /**
     * 获取当天剩余时间 秒
     *
     * @return
     */
    public static int getTodayResideSec() {
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        return (int) LocalDateTime.now().until(midnight, ChronoUnit.SECONDS);
    }

    /**
     * 获取当天已过时间 秒
     *
     * @return
     */
    public static int getTodayPassedSec() {
        LocalDateTime now = LocalDateTime.now();
        return (int) LocalDateTime.of(now.toLocalDate(), LocalTime.MIN).until(now, ChronoUnit.SECONDS);
    }

    /**
     * 获取本小时剩余时间 秒
     *
     * @return
     */
    public static int getThisHourResideSec() {
        LocalDateTime nextHour = LocalDateTime.now().plusHours(1).withMinute(0).withSecond(0).withNano(0);
        return (int) LocalDateTime.now().until(nextHour, ChronoUnit.SECONDS);
    }

    /**
     * 获取半年前时间
     *
     * @return
     */
    public static Date getHalfYearDate() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -6);
        return c.getTime();
    }

    /**
     * 获取day天前的时间
     *
     * @param day
     * @return
     */
    public static Date getDayBeforeDate(Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -day);
        return c.getTime();
    }

    /**
     * 获取第几天前0点时间戳
     *
     * @param past
     * @return
     */
    public static Long getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);

        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format.parse(result, pos);
        return strtodate.getTime() / 1000;
    }

    /**
     * 获取第几月前0点时间戳
     *
     * @param past
     * @return
     */
    public static Long getPastMonth(int past) {
        Date date = DateUtils.addMonths(new Date(), -past);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    public static Date getStartDayOfWeek(LocalDate localDate) {
        LocalDate newDate = localDate.with(DayOfWeek.MONDAY);
        return Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date getEndDayOfWeek(LocalDate localDate) {
        LocalDate newDate = localDate.with(DayOfWeek.SUNDAY);
        return Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).plusDays(1L).minusNanos(1L).toInstant());
    }

    public static String timestampToDate(String format, long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(timestamp);
    }

    /**
     * @return long
     * @Title 获取传入时间的最近8AM的时间戳
     * @Author ting
     * @Description TODO
     * @Date 2019-03-14 14:43:49
     * @Param [timeStampInMillis]
     **/
    public static long getNearest1DayOn8AmTimestampByTimestamp(long timestampInMillis) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestampInMillis);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour <= 8) {
            calendar.set(Calendar.DAY_OF_MONTH, -1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * @return long
     * @Title 获取传入时间的最近00, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55分的时间戳
     * @Author ting
     * @Description TODO
     * @Date 2019-03-14 14:45:03
     * @Param [timeStampInMillis]
     **/
    public static long getNearest5MinutesTimestampByTimestamp(long timestampInMillis) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestampInMillis);
        calendar.set(Calendar.MINUTE, -5);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 得到分钟
        int minute = calendar.get(Calendar.MINUTE);
        int count = 0;
        if (minute != 0
                && minute != 5
                && minute != 10
                && minute != 15
                && minute != 20
                && minute != 25
                && minute != 30
                && minute != 35
                && minute != 40
                && minute != 45
                && minute != 50
                && minute != 55) {
            //时间不为00,5,10,15,20,25,30,35,40,45,50,55的情况
            //判断距离最近的00，5,10,15,20,25,30,35,40,45,50,55的分钟数
            for (int i = minute; i >= 0; i--) {
                if (i % 5 == 0) {
                    break;
                } else {
                    count++;
                }
            }
        }
        //获取最近的分钟
        calendar.add(Calendar.MINUTE, -count);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取最近的5分钟整点
     *
     * @param timestampInMillis
     * @return
     */
    public static Integer getNearest5MinutesByTimestamp(long timestampInMillis) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestampInMillis);
        // 得到分钟
        int minute = calendar.get(Calendar.MINUTE);
        int count = minute % 5;
        calendar.add(Calendar.MINUTE, -count);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Long time = calendar.getTimeInMillis() / 1000;
        return time.intValue();
    }

    /**
     * 获取n分钟钱时间戳
     *
     * @param n
     * @return
     */
    public static Integer getBeforeMinutes(long timestampInMillis, int n) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestampInMillis);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Long time = calendar.getTimeInMillis() / 1000 - n * 60;
        return time.intValue();
    }

    /**
     * 获取n分钟钱时间戳
     *
     * @param n
     * @return
     */
    public static Integer getAfterMinutes(long timestampInMillis, int n) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestampInMillis);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Long time = calendar.getTimeInMillis() / 1000 + n * 60;
        return time.intValue();
    }

    public static long getDate8AMTimestamp(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.add(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取某周的开始日期
     *
     * @param offset 0本周，1下周，-1上周，依次类推
     * @return
     */
    public static long weekStart(int offset) {
        LocalDate localDate = LocalDate.now().plusWeeks(offset);
        return localDate.with(DayOfWeek.MONDAY).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    public static long weekEnd(int offset) {
        LocalDate localDate = LocalDate.now().plusWeeks(offset);
        return localDate.with(DayOfWeek.SUNDAY).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }


    /**
     * 获取某月的开始日期
     *
     * @param offset 0本月，1下个月，-1上个月，依次类推
     * @return
     */
    public static long monthStart(int offset) {
        return LocalDate.now().plusMonths(offset).with(TemporalAdjusters.firstDayOfMonth())
                .atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    public static long monthEnd(int offset) {
        return LocalDate.now().plusMonths(offset).with(TemporalAdjusters.lastDayOfMonth())
                .atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    /**
     * 比较是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * @return long 毫秒
     * @Title TODO
     * @Author ting
     * @Description 获取传入日期时间戳的当日开始时间戳
     * @Date 2019-03-25 14:46:15
     * @Param [dateTimeInMillis]
     **/
    public static long getDateStartTime(long dateTimeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(dateTimeInMillis);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static Integer getDateStartTime(int time) {
        int i = time + 8 * HOUR_SECOND;
        if (i % DAY_SECOND == 0) {
            return time;
        }
        return time - i % DAY_SECOND;
    }

    /**
     * @return long 毫秒
     * @Title TODO
     * @Author ting
     * @Description 获取传入日期时间戳的当日结束时间戳
     * @Date 2019-03-25 14:46:29
     * @Param [dateTimeInMillis]
     **/
    public static long getDateEndTime(long dateTimeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(dateTimeInMillis);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获得昨日该时刻的时间戳
     *
     * @param time
     * @return
     */
    public static Integer getYesterdayTime(Integer time) {
        return time - DAY_SECOND;
    }

    /**
     * 获得当前时间的时间戳
     *
     * @return
     */
    public static Integer getNowTime() {
        Long i = System.currentTimeMillis() / 1000;
        return i.intValue();
    }

    /**
     * 获得当前时间一天后时间戳
     *
     * @return
     */
    public static Long getAfterOneDayTime() {
        return System.currentTimeMillis() + 86400;
    }

    /**
     * 根据入参时间戳获取年月日时间戳
     *
     * @param date
     * @return
     */
    public static long getYMDtime(long date) {
        LocalDate localDate = Instant.ofEpochMilli(date * 1000).atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }


    /**
     * 格式化String时间
     *
     * @param time       String类型时间
     * @param timeFormat String类型格式
     * @return 格式化后的Date日期
     */
    public static Date parseStrToDate(String time, String timeFormat) {
        if (time == null || time.equals("")) {
            return null;
        }

        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFormat);
            date = dateFormat.parse(time);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 格式化Date时间
     *
     * @param time         Date类型时间
     * @param timeFormat   String类型格式
     * @param defaultValue 默认时间值String类型
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFormat, final String defaultValue) {
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFormat);
            return dateFormat.format(time);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Integer parseTimeToStandardHour(Integer time) {
        int i = time % 3600;
        if (i == 0) {
            return time;
        } else {
            return time - i;
        }
    }

    /**
     * @return
     */
    public static Integer dateToStamp(String s, String dateType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateType);
        Date parse;
        try {
            parse = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            return 0;
        }
        Long l = parse.getTime() / 1000;
        return l.intValue();
    }

    public static Integer getCurrentHourStartTime() {
        SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Date now = new Date();
        try {
            now = longHourSdf.parse(longHourSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Long l = now.getTime() / 1000;
        return l.intValue();
    }

    /**
     * 得到一小时之前的整点时间
     *
     * @return
     */
    public static Integer getBeforeHour() {
        return getCurrentHourStartTime() - 3600;
    }

    /**
     * 判断是不是一天的整点
     *
     * @param time
     * @return
     */
    public static Boolean isInt1Day(Integer time) {
        return time.equals(getZeroStart(time));
    }

    public static Boolean isInt1hour(Integer time) {
        int i = 3600;
        int pastDate = getZeroStart(time);
        int i1 = time - pastDate;
        return i1 % i == 0;
    }

    public static Boolean isInt4hour(Integer time) {
        int i = 3600 * 4;
        int pastDate = getZeroStart(time);
        int i1 = time - pastDate;
        return i1 % i == 0;
    }

    public static Boolean isInt6hour(Integer time) {
        int i = 6;
        int pastDate = getZeroStart(time);
        int i1 = time - pastDate;
        return (i1 / 3600) % i == 2;
    }

    public static Integer getZeroStart(Integer time) {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(Long.valueOf(time) * 1000L);
        ca.set(Calendar.HOUR, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        Long l = ca.getTimeInMillis() / 1000;
        return l.intValue();
    }

    public static Integer getHourBegin(Integer time) {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(Long.valueOf(time) * 1000L);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        Long l = ca.getTimeInMillis() / 1000;
        return l.intValue();
    }

    public static Integer getDayBegin(Integer time) {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(Long.valueOf(time) * 1000L);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        Long l = ca.getTimeInMillis() / 1000;
        return l.intValue();
    }

    /**
     * 获取几天前的整点时间戳
     *
     * @param i
     * @return
     */
    public static Integer getBeforedayHour(int i) {
        return getCurrentHourStartTime() - DAY_SECOND * i;
    }

    public static String getGeneralNowTime() {
        return LocalDateTime.now().format(DATE_TIME_FORMATTER);
    }

    public static Integer getMinuteStartTime() {
        return (int) LocalDateTime.now().atZone(ZoneId.systemDefault()).withSecond(0).toEpochSecond();
    }

    /**
     * 获取两个时间相差几年
     *
     * @return
     */
    public static Integer getYearPast(Integer time, Integer time2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.valueOf(time) * 1000L);
        int year = calendar.get(Calendar.YEAR);
        calendar.setTimeInMillis(Long.valueOf(time2) * 1000L);
        int year2 = calendar.get(Calendar.YEAR);
        return Math.abs(year2 - year);
    }

    /**
     * 获取当年的开始时间戳
     *
     * @return
     */
    public static Long getYearStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 时间戳转LocalDate（本地时区）
     *
     * @param timeStampSecond 单位:秒
     * @return
     */
    public static LocalDate toLocalDate(long timeStampSecond) {
        return Instant.ofEpochSecond(timeStampSecond).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 时间戳转LocalDateTime（本地时区）
     *
     * @param timeStampSecond 单位:秒
     * @return
     */
    public static LocalDateTime toLocalDateTime(long timeStampSecond) {
        return Instant.ofEpochSecond(timeStampSecond).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Integer getInt4hour(Integer time) {
        int i = 14400;
        int remainder = time % i;
        return remainder == 0 ? time : time - remainder;
    }

    /**
     * 获取今日0点时间戳（秒）
     *
     * @return
     */
    public static long getTimeStampAtZeroOclock() {
        return Timestamp.valueOf(LocalDateTime.of(LocalDate.now(), LocalTime.MIN)).getTime() / 1000;
    }

    /**
     * 获取今日指定时钟的时间戳（秒）
     *
     * @param hour the hour-of-day to represent, from 0 to 23
     * @return
     */
    public static long getTimeStampAt(int hour) {
        return Timestamp.valueOf(LocalDateTime.of(LocalDate.now(), LocalTime.of(hour, 0))).getTime() / 1000;
    }

    /**
     * 获取本月的开始时间
     *
     * @param time
     * @return
     */
    public static Integer getMonthBegin(Integer time) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(Long.valueOf(time) * 1000L));
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Long l = c.getTimeInMillis() / 1000L;
        return l.intValue();
    }

    public static boolean isMonthBegin(Integer time) {
        return time.equals(getMonthBegin(time));
    }

    /**
     * 获取半小时前时间
     *
     * @return
     */
    public static Date getHalfHourDate() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, -30);
        return c.getTime();
    }

    public static long getTimeStampByStr(String time) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date date = null;
        try {
            //设置要读取的时间字符串格式
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //转换为Date类
        Long timestamp = date.getTime();
        //获得时间戳
        return timestamp / 1000;
    }

    /**
     * 将字符串转位日期类型
     *
     * @param sdate
     * @return
     */
    public static Date strToDate(String sdate, SimpleDateFormat dateFormater) {
        try {
            return dateFormater.parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }

}
